/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ow.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ow.entity.OwIntention;
import com.kaaction.modules.ow.service.OwIntentionService;

/**
 * 意向Controller
 * @author huanghaiming
 * @version 2017-06-27
 */
@Controller
@RequestMapping(value = "${adminPath}/ow/owIntention")
public class OwIntentionController extends BaseController {

	@Autowired
	private OwIntentionService owIntentionService;
	
	@ModelAttribute
	public OwIntention get(@RequestParam(required=false) String id) {
		OwIntention entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = owIntentionService.get(id);
		}
		if (entity == null){
			entity = new OwIntention();
		}
		return entity;
	}
	
	@RequiresPermissions("ow:owIntention:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwIntention owIntention, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OwIntention> page = owIntentionService.findPage(new Page<OwIntention>(request, response), owIntention); 
		model.addAttribute("page", page);
		return "modules/ow/owIntentionList";
	}

	@RequiresPermissions("ow:owIntention:view")
	@RequestMapping(value = "form")
	public String form(OwIntention owIntention, Model model) {
		model.addAttribute("owIntention", owIntention);
		return "modules/ow/owIntentionForm";
	}

	@RequiresPermissions("ow:owIntention:edit")
	@RequestMapping(value = "save")
	public String save(OwIntention owIntention, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, owIntention)){
			return form(owIntention, model);
		}
		owIntentionService.save(owIntention);
		addMessage(redirectAttributes, "保存意向成功");
		return "redirect:"+Global.getAdminPath()+"/ow/owIntention/?repage";
	}
	
	@RequiresPermissions("ow:owIntention:edit")
	@RequestMapping(value = "delete")
	public String delete(OwIntention owIntention, RedirectAttributes redirectAttributes) {
		owIntentionService.delete(owIntention);
		addMessage(redirectAttributes, "删除意向成功");
		return "redirect:"+Global.getAdminPath()+"/ow/owIntention/?repage";
	}

}